<script setup>
import { ref } from 'vue'
import axios from 'axios'
import { useRouter } from 'vue-router'
import topLogo from '../assets/logo.png'        // 顶部品牌logo
import cardLogo from '../assets/logo.png'       // 登录框内logo
import loginImg from '../assets/登录log.jpeg'    // 右侧图片

const router = useRouter()
const form = ref({ username: '', password: '' })
const errorMsg = ref('')
const loading = ref(false)

async function handleLogin() {
  errorMsg.value = ''
  loading.value = true
  try {
    const res = await axios.post('/api/user/login', form.value)
    if (res.data.code === 0) {
      localStorage.setItem('loginUser', JSON.stringify(res.data.data))
      router.push('/route-list')
    } else {
      errorMsg.value = res.data.msg
    }
  } catch (e) {
    errorMsg.value = '网络异常，请稍后重试'
  } finally {
    loading.value = false
  }
}
function toRegister() {
  router.push('/register')
}
</script>

<template>
  <div class="login-bg">
    <!-- 顶部大LOGO -->
    <div class="header-logo">
      <img :src="topLogo" class="top-logo" alt="LOGO" />
    </div>
    <!-- 合并卡片 -->
    <el-card class="login-card-combined">
      <div class="login-flex-box">
        <!-- 左侧登录表单 -->
        <div class="login-form-col">
          <div class="card-logo-wrap">
            <img :src="cardLogo" class="card-logo" alt="logo" />
          </div>
          <h2 class="login-title">用户登录</h2>
          <el-form :model="form" label-width="70px" @submit.prevent>
            <el-form-item label="用户名">
              <el-input v-model="form.username" placeholder="请输入用户名" clearable />
            </el-form-item>
            <el-form-item label="密码">
              <el-input v-model="form.password" type="password" placeholder="请输入密码" clearable show-password />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" :loading="loading" @click="handleLogin" style="width:100%;">登录</el-button>
            </el-form-item>
            <el-form-item>
              <el-link type="info" @click="toRegister">还没有账号？注册</el-link>
            </el-form-item>
            <div style="margin-top:-12px; text-align:center;">
              <span v-if="errorMsg" style="color:#e56c6c;">{{ errorMsg }}</span>
            </div>
          </el-form>
        </div>
        <!-- 右侧图片 -->
        <div class="login-img-col">
          <img :src="loginImg" class="login-img" alt="登录侧图" />
        </div>
      </div>
    </el-card>
  </div>

</template>

<style scoped>
.login-bg {
  min-height: 100vh;
  background: #f6f7fb;
  padding-top: 48px;
}
.header-logo {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  height: 110px;
  margin-bottom: 18px;
}
.top-logo {
  height: 88px;
  width: auto;
  object-fit: contain;
  margin-top: 14px;
}

.login-card-combined {
  max-width: 820px;
  margin: 0 auto;
  padding: 0;
  border-radius: 18px;
  box-shadow: 0 2px 32px rgba(60,60,60,0.11);
  background: #fff;
}

.login-flex-box {
  display: flex;
  justify-content: center;
  align-items: stretch;
  min-height: 420px;
}

.login-form-col {
  flex: 1.1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 36px 34px 28px 38px;
  border-right: 1px solid #f0f1f5;
  min-width: 320px;
  background: #fff;
}

.card-logo-wrap {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 10px;
}
.card-logo {
  height: 52px;
  width: auto;
  object-fit: contain;
}

.login-title {
  text-align: center;
  margin: 18px 0 28px 0;
  font-size: 2rem;
  font-weight: 700;
  color: #23272e;
}

.login-img-col {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 30px 18px 30px 18px;
  background: #fff;
  min-width: 270px;
}
.login-img {
  width: 260px;
  max-width: 90%;
  max-height: 320px;
  border-radius: 14px;
  object-fit: cover;
  box-shadow: 0 1px 10px rgba(120,120,120,0.11);
}

@media (max-width: 900px) {
  .login-card-combined {
    max-width: 97vw;
  }
  .login-flex-box {
    flex-direction: column;
    align-items: center;
    min-height: unset;
  }
  .login-form-col {
    border-right: none;
    border-bottom: 1px solid #f0f1f5;
    min-width: unset;
    width: 100%;
    padding: 32px 16px 18px 16px;
  }
  .login-img-col {
    min-width: unset;
    width: 100%;
    justify-content: center;
    padding: 16px 0 18px 0;
  }
  .login-img {
    width: 170px;
    max-width: 95vw;
  }
}

</style>